package cn.site.modules.sys.utils;

import cn.site.core.utils.SpringContextHolder;
import cn.site.core.utils.StringUtils;
import cn.site.modules.sys.dao.LogDao;
import cn.site.modules.sys.entity.Log;

import javax.servlet.http.HttpServletRequest;

/**
 * 日志工具类
 */
public class LogUtils {

    private static LogDao logDao = SpringContextHolder.getBean(LogDao.class);
    //private static MenuDao menuDao = SpringContextHolder.getBean(MenuDao.class);

    /*保存日志*/
    public static void saveLog(HttpServletRequest request, String title){

    }

    /*保存日志*/
    public static void saveLog(HttpServletRequest request, Object handler, Exception ex, String title){

    }

    /*获取菜单名称路径（如：系统设置-机构用户-用户管理-编辑）*/
    public static String getMenuNamePath(String requestUri, String permission){

        return "";
    }

    /*保存日志线程*/
    public static class SaveLogThread extends Thread{
        private Log log;
        private Object handler;
        private Exception ex;

        public SaveLogThread(Log log, Object handler, Exception ex){
            super(SaveLogThread.class.getSimpleName());
            this.log = log;
            this.handler = handler;
            this.ex = ex;
        }

        @Override
        public void run() {
            /*// 获取日志标题
            if (StringUtils.isBlank(log.getTitle())) {
                String permission = "";
                if (handler instanceof HandlerMethod) {
                    Method m = ((HandlerMethod) handler).getMethod();
                    RequiresPermissions rp = m.getAnnotation(RequiresPermissions.class);
                    permission = (rp != null ? StringUtils.join(rp.value(), ",") : "");
                }
                log.setTitle(getMenuNamePath(log.getRequestUri(), permission));
            }
            // 如果有异常，设置异常信息
            log.setException(Exceptions.getStackTraceAsString(ex));
            // 如果无标题并无异常日志，则不保存信息
            if (StringUtils.isBlank(log.getTitle()) && StringUtils.isBlank(log.getException())) {
                return;
            }
            // 保存日志信息
            log.preInsert();
            logDao.insert(log);*/
        }
    }

}
